package com.wxy.ej.flow.modules.act.listen;

import com.rt.easyjava.common.utils.others.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Slf4j
@Component("instanceCompleteTaskListener")
@Scope("prototype")
public class InstanceCompleteTaskListener {

    public boolean execCmpyInstance(ExecutionEntity execution){
        return execCmpyInstance(execution, "");
    }

    public boolean execCmpyInstance(ExecutionEntity execution, String varPre){
        log.info("---->act listener: InstanceCompleteTaskListener");

        System.out.println("=== InstanceCompleteTaskListener - execCmpyInstance ====");
        System.out.println("nrOfInstancesLocal:" + execution.getVariableLocal("nrOfInstances"));
        System.out.println("nrOfCompletedInstancesLocal:" + execution.getVariableLocal("nrOfCompletedInstances"));
        int nrOfInstances = Integer.parseInt(execution.getVariableLocal("nrOfInstances").toString());
        int nrOfCompletedInstances = Integer.parseInt(execution.getVariableLocal("nrOfCompletedInstances").toString());

        // getVariableLocal 取不到值
        Integer cmpyConditionRadio = (Integer) execution.getVariable(varPre+"cmpyConditionRadio");
        Integer cmpyConditionCount = (Integer) execution.getVariable(varPre+"cmpyConditionCount");
        String cmpyConditionRate = (String) execution.getVariable(varPre+"cmpyConditionRate");
        System.out.println("cmpyConditionRadio:"+cmpyConditionRadio);
        System.out.println("cmpyConditionCount:"+cmpyConditionCount);
        System.out.println("cmpyConditionRate:"+cmpyConditionRate);
        if (StringUtils.isNull(cmpyConditionRadio)) {
            if (nrOfCompletedInstances == 1) {
                return true;
            }
        }
        else if (cmpyConditionRadio.intValue() == 1) {
            if (nrOfCompletedInstances == cmpyConditionCount.intValue()) {
                return true;
            }
        }
        else if (cmpyConditionRadio.intValue() == 2) {
            // 结果是:   -1：小于；   0 ：等于；   1 ：大于；
            if ( new BigDecimal(nrOfCompletedInstances).divide(new BigDecimal(nrOfInstances), 2, BigDecimal.ROUND_HALF_UP).compareTo(new BigDecimal(cmpyConditionRate)) >=0) {
                return true;
            }
        }

        return false;
    }

    public boolean execUserInstance(ExecutionEntity execution){
        return execUserInstance(execution, "");
    }

    public boolean execUserInstance(ExecutionEntity execution, String varPre){
        System.out.println("=== InstanceCompleteTaskListener - execUserInstance ====");
        System.out.println("nrOfInstancesLocal:" + execution.getVariableLocal("nrOfInstances"));
        System.out.println("nrOfCompletedInstancesLocal:" + execution.getVariableLocal("nrOfCompletedInstances"));
        int nrOfInstances = Integer.parseInt(execution.getVariableLocal("nrOfInstances").toString());
        int nrOfCompletedInstances = Integer.parseInt(execution.getVariableLocal("nrOfCompletedInstances").toString());

        // getVariableLocal 取不到值
        Integer userConditionRadio = (Integer) execution.getVariable(varPre+"userConditionRadio");
        Integer userConditionCount = (Integer) execution.getVariable(varPre+"userConditionCount");
        String userConditionRate = (String) execution.getVariable(varPre+"userConditionRate");
        System.out.println("userConditionRadio:"+userConditionRadio);
        System.out.println("userConditionCount:"+userConditionCount);
        System.out.println("userConditionRate:"+userConditionRate);
        if (StringUtils.isNull(userConditionRadio)) {
            if (nrOfCompletedInstances == 1) {
                return true;
            }
        }
        else if (userConditionRadio.intValue() == 1) {
            if (nrOfCompletedInstances == userConditionCount.intValue()) {
                return true;
            }
        }
        else if (userConditionRadio.intValue() == 2) {
            // 结果是:   -1：小于；   0 ：等于；   1 ：大于；
            if (new BigDecimal(nrOfCompletedInstances).divide(new BigDecimal(nrOfInstances), 2, BigDecimal.ROUND_HALF_UP).compareTo(new BigDecimal(userConditionRate)) >=0) {
                return true;
            }
        }

        return false;
    }


}
